package cn.getech.data.metadata.service.impl;

import cn.getech.data.development.api.DefaultSecurityDataApi;
import cn.getech.data.metadata.entity.CollectionNotificationHistory;
import cn.getech.data.metadata.entity.CollectionNotificationReceived;
import cn.getech.data.metadata.mapper.CollectionNotificationHistoryMapper;
import cn.getech.data.metadata.service.CollectionNotificationHistoryService;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("collectionNotificationHistoryServiceImpl")
public class CollectionNotificationHistoryServiceImpl  extends ServiceImpl<CollectionNotificationHistoryMapper, CollectionNotificationHistory> implements CollectionNotificationHistoryService {

    @Autowired
    private CollectionNotificationHistoryMapper collectionNotificationHistoryMapper;

    @Override
    @Transactional
    public void saveCollectionNotificationHistory(CollectionNotificationHistory collectionNotificationHistory) {
        collectionNotificationHistory.setCreateUser(ShiroUtils.getUserId());
        collectionNotificationHistory.setCreateTime(new Date());
        collectionNotificationHistory.setTenantId(ShiroUtils.getTenantId());
        this.save(collectionNotificationHistory);
    }

    @Override
    @Transactional
    public void updateCollectionNotificationHistory(ArrayList<Long> updateId, CollectionNotificationHistory collectionNotificationHistory) {
       if(CollectionUtil.isNotEmpty(updateId)){
           for(Long updateid:updateId){
               CollectionNotificationHistory collection=new CollectionNotificationHistory();
               BeanUtils.copyProperties(collectionNotificationHistory,collection);
               collection.setId(updateid);
               collectionNotificationHistory.setUpdateUser(ShiroUtils.getUserId());
               collectionNotificationHistory.setUpdateTime(new Date());
               collectionNotificationHistory.setTenantId(ShiroUtils.getTenantId());
               this.updateById(collectionNotificationHistory);
           }
       }
       return;
    }

    @Override
    @Transactional
    public void updateCollectionNotificationHistory(CollectionNotificationHistory collectionNotificationHistory) {
        this.updateById(collectionNotificationHistory);
    }

    @Override
    public List<CollectionNotificationHistory> queryCollectionNotificationHistory(String type) {
        LambdaQueryWrapper<CollectionNotificationHistory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CollectionNotificationHistory::getNotificationFlag,type);
        return this.list(queryWrapper);
    }

    @Override
    public List<CollectionNotificationHistory> queryCollectionNotificationHistorybyType(CollectionNotificationHistory collectionNotificationHistory) {
        LambdaQueryWrapper<CollectionNotificationHistory> queryWrapper = Wrappers.lambdaQuery();
        if(collectionNotificationHistory!=null&&collectionNotificationHistory.getNotificationFlag()!=null){
            queryWrapper.eq(CollectionNotificationHistory::getNotificationFlag,collectionNotificationHistory.getNotificationFlag());
        }
        if(collectionNotificationHistory!=null&&StringUtils.isNotEmpty(collectionNotificationHistory.getRelatednessType())){
            queryWrapper.eq(CollectionNotificationHistory::getRelatednessType,collectionNotificationHistory.getRelatednessType());
        }
        if(collectionNotificationHistory!=null&&collectionNotificationHistory.getNotificationReceivedId()!=null){
            queryWrapper.eq(CollectionNotificationHistory::getNotificationReceivedId,collectionNotificationHistory.getNotificationReceivedId());
        }
        return this.list(queryWrapper);
    }
}
